package controller;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import pojo.Book;
import pojo.Car;
import pojo.CarStore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;
import java.util.Map;

// @Controller 注解表示了这个类是一个 Handler
@Controller
public class FirstController {

    // @RequestMapping 注解表示了该 url 路径，及请求的方法类型
    @RequestMapping(value = "/getNowTime", method = {RequestMethod.POST})
    public ModelAndView getNowTime() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("showNowTime");
        modelAndView.addObject("time", System.currentTimeMillis());
        return modelAndView;
    }

    @RequestMapping(value = "/getBook")
    public String getBook(Book book) {
        System.out.println(book);

        return "showNowTime";
    }

    @RequestMapping(value = "/printError")
    public String printError() {
        int a = 1 / 0;

        return "";
    }

    @RequestMapping(value = "/uploadFile")
    public String uploadFile(MultipartFile uploadFile) throws Exception{
        String strName = uploadFile.getOriginalFilename();
        System.out.println(strName);

        String strPath = "D:\\tomcat\\wwwroot\\";
        String strFileName = strPath + System.currentTimeMillis() + ".txt";
        File file = new File(strFileName);
        uploadFile.transferTo(file);

        return "showNowTime";
    }

    @RequestMapping(value = "/getJson")
    public @ResponseBody Book getJson(@RequestBody Book book) {

        System.out.println(book);

        return book;
    }

    @RequestMapping(value = "/getParamToJson")
    public @ResponseBody Book getParamToJson(@RequestBody Book book) {

        System.out.println(book);

        return book;
    }

    @RequestMapping(value = "/convertDate")
    public String convertDate(Book book) {
        System.out.println(book);

        return "showNowTime";
    }

    //@Validated 标记了这个 Pojo 需要进行检查，BindingResult 和该注解是配对出现的，用于查看是否通过校验
    @RequestMapping(value = "/validatorTest")
    public String validatorTest(@Validated Car car, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            List<ObjectError> listError = bindingResult.getAllErrors();
            for (ObjectError error : listError) {
                System.out.println(error.getDefaultMessage());
            }
        }


        System.out.println(car);

        return "showNowTime";
    }

    //获得基本数据结构的数组, 其中 RequestParam 是用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容
    @RequestMapping(value = "/getSimpleArray")
    public String getSimpleArray(@RequestParam(value = "ids") int[] arrId) {

        for (int a : arrId) {
            System.out.println(a);
        }

        return "showNowTime";
    }

    @RequestMapping(value = "/getSimpleList")
    public String getSimpleList(@RequestParam(value = "ids") List<Integer> listId) {

        for (int a : listId) {
            System.out.println(a);
        }

        return "showNowTime";
    }

    //RequestBody 用于处理 application/json 类型的参数
    @RequestMapping(value = "/getPojoArray")
    public String getPojoArray(@RequestBody Car[] arrCar) {

        for (Car car : arrCar) {
            System.out.println(car);
        }

        return "showNowTime";
    }

    @RequestMapping(value = "/getPojoList")
    public String getPojoList(@RequestBody List<Car> lisrCar) {

        for (Car car : lisrCar) {
            System.out.println(car);
        }

        return "showNowTime";
    }

    @RequestMapping(value = "/getPojoMap")
    public String getPojoMap(@RequestBody List<Map<String, Object>> listMap) {

        for (Map<String, Object> mapItem : listMap) {
            System.out.println(mapItem);
        }

        return "showNowTime";
    }

    //Pojo 中包含了 List 等集合类型的使用方式
    @RequestMapping(value = "/getPojoWithList")
    public String getPojoWithList(@RequestBody CarStore carStore) {

        System.out.println(carStore.getName());

        List<Car> listCar = carStore.getListCar();
        for (Car car : listCar) {
            System.out.println(car);
        }

        return "showNowTime";
    }

    @RequestMapping(value = "/returnVoid")
    public void returnVoid(HttpServletRequest request, HttpServletResponse response) {

    }

    @RequestMapping(value = "/getInt")
    public String getInt(@RequestParam(value = "id") int ids) {
        // 使用 @RequestParam 来完成简单类型参数的映射，此时表单键名为 id, 将值映射到 ids 上

        return "showNowTime";
    }

    //使用 RESTful 风格的 Url。使用 {???} 将资源标识包装。在参数中使用 PathVariable 进行参数的绑定
    @RequestMapping(value = "/RESTfulTest/{id}")
    public String testRESTful(@PathVariable("id") int id) {
        System.out.println(id);

        return "showNowTime";
    }

}
